<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>原型与原型链 | 小飞侠Pan的Blog</title><meta name="keywords" content="JS,原型链"><meta name="author" content="潘静怡"><meta name="copyright" content="潘静怡"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一.原型[prototype]1.函数的prototype属性 并不是所有对象都能有prototype,只有函数才有prototype属性，每个函数都有一个prototype属性, 它默认指向一个Object空对象(即称为: 原型对象)  prototype也是一个对象，而且默认包含一个constructor属性，这个constructor属性指向函数自己 123456function Perso">
<meta property="og:type" content="article">
<meta property="og:title" content="原型与原型链">
<meta property="og:url" content="https://mengqiuleo.gitee.io/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/index.html">
<meta property="og:site_name" content="小飞侠Pan的Blog">
<meta property="og:description" content="一.原型[prototype]1.函数的prototype属性 并不是所有对象都能有prototype,只有函数才有prototype属性，每个函数都有一个prototype属性, 它默认指向一个Object空对象(即称为: 原型对象)  prototype也是一个对象，而且默认包含一个constructor属性，这个constructor属性指向函数自己 123456function Perso">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg">
<meta property="article:published_time" content="2022-02-26T10:42:32.957Z">
<meta property="article:modified_time" content="2022-02-26T11:36:52.728Z">
<meta property="article:author" content="潘静怡">
<meta property="article:tag" content="JS">
<meta property="article:tag" content="原型链">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg"><link rel="shortcut icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg"><link rel="canonical" href="https://mengqiuleo.gitee.io/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":500},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '原型与原型链',
  isPost: true,
  isHome: false,
  isHighlightShrink: undefined,
  isToc: true,
  postUpdate: '2022-02-26 19:36:52'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/social.css" type="text/css"><link rel="stylesheet" href="/css/style.css" type="text/css"><meta name="generator" content="Hexo 5.4.1"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/book/"><i class="fa-fw fa-solid fa-bookmark"></i><span> 前端知识库</span></a></div><div class="menus_item"><a class="site-page" href="/work/"><i class="fa-fw fa fa-heartbeat"></i><span> 在线简历</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa-solid fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小飞侠Pan的Blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/book/"><i class="fa-fw fa-solid fa-bookmark"></i><span> 前端知识库</span></a></div><div class="menus_item"><a class="site-page" href="/work/"><i class="fa-fw fa fa-heartbeat"></i><span> 在线简历</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fa-solid fa-paper-plane"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">原型与原型链</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-26T10:42:32.957Z" title="发表于 2022-02-26 18:42:32">2022-02-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-26T11:36:52.728Z" title="更新于 2022-02-26 19:36:52">2022-02-26</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JS%E9%AB%98%E7%BA%A7/">JS高级</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>13分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="原型与原型链"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div><article class="post-content" id="article-container"><h3 id="一-原型-prototype"><a href="#一-原型-prototype" class="headerlink" title="一.原型[prototype]"></a>一.原型[prototype]</h3><h4 id="1-函数的prototype属性"><a href="#1-函数的prototype属性" class="headerlink" title="1.函数的prototype属性"></a>1.函数的<code>prototype</code>属性</h4><ul>
<li><p>并不是所有对象都能有prototype,只有函数才有prototype属性，每个函数都有一个prototype属性, 它默认指向一个Object空对象(即称为: 原型对象)</p>
</li>
<li><p><strong>prototype也是一个对象，而且默认包含一个constructor属性，这个constructor属性指向函数自己</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Person</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span>=<span class="string">&#x27;my name&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> p=<span class="keyword">new</span> <span class="title class_">Person</span>();</span><br><span class="line"><span class="comment">//函数的prototype.constructor指向自己</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Person</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">constructor</span>===<span class="title class_">Person</span>);<span class="comment">//true</span></span><br></pre></td></tr></table></figure></li>
<li><p>原型对象中有一个属性constructor, 它指向函数对象</p>
<p><em>constructor：当一个函数被用来构造一个对象时，那这个函数就被成为constructor</em></p>
<p><strong>实例的构造函数属性（constructor）指向构造函数。</strong></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Person</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span>=<span class="string">&#x27;my name&#x27;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> p=<span class="keyword">new</span> <span class="title class_">Person</span>();</span><br><span class="line"><span class="comment">//函数的prototype.constructor指向自己</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Person</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">constructor</span>===<span class="title class_">Person</span>);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//constructor：当一个函数被用来构造一个对象时，那这个函数就被成为constructor</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(p.<span class="property">constructor</span>);</span><br><span class="line"><span class="comment">// ƒ Person()&#123;</span></span><br><span class="line"><span class="comment">//     this.name=&#x27;my name&#x27;;</span></span><br><span class="line"><span class="comment">// &#125;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
<li><p><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202261843696.png"></p>
</li>
<li><p>给原型对象添加属性(<code>一般都是方法</code>)</p>
<p>作用: 函数的所有实例对象自动拥有原型中的属性(方法)</p>
<p>代码示例：</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 每个函数都有一个prototype属性, 它默认指向一个Object空对象(即称为: 原型对象)</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Date</span>.<span class="property"><span class="keyword">prototype</span></span>, <span class="keyword">typeof</span> <span class="title class_">Date</span>.<span class="property"><span class="keyword">prototype</span></span>)</span><br><span class="line">   <span class="keyword">function</span> <span class="title function_">Fun</span> () &#123; &#125;</span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Fun</span>.<span class="property"><span class="keyword">prototype</span></span>)  <span class="comment">// 默认指向一个Object空对象(没有我们的属性)</span></span><br><span class="line"></span><br><span class="line">   <span class="comment">// 原型对象中有一个属性constructor, 它指向函数对象</span></span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Date</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">constructor</span>===<span class="title class_">Date</span>)</span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Fun</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">constructor</span>===<span class="title class_">Fun</span>)</span><br><span class="line"></span><br><span class="line">   <span class="comment">//给原型对象添加属性(一般是方法) ===&gt;实例对象可以访问</span></span><br><span class="line">   <span class="title class_">Fun</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">test</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123; <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;test()&#x27;</span>) &#125;</span><br><span class="line">   <span class="keyword">var</span> fun = <span class="keyword">new</span> <span class="title class_">Fun</span>()</span><br><span class="line">   fun.<span class="title function_">test</span>()</span><br></pre></td></tr></table></figure>

<h4 id="2-显示原型与隐式原型"><a href="#2-显示原型与隐式原型" class="headerlink" title="2.显示原型与隐式原型"></a>2.显示原型与隐式原型</h4><p>总结：<strong>只有函数才有prototype属性，而_*proto_*属性却是所有对象都有的</strong>。</p>
<ol>
<li><p>每个构造函数function都有一个<code>prototype</code>，即<code>显式</code>原型(属性)</p>
</li>
<li><p>每个对象都有一个[<code>__ proto __</code>]，可称为<code>隐式</code>原型(属性)</p>
</li>
<li><p>实例对象的隐式原型的值为其对应构造函数的显式原型的值</p>
</li>
</ol>
<p><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202261843699.png"></p>
<p>  4.总结:</p>
<ul>
<li>函数的[<code>prototype</code>]属性: 在定义函数时自动添加的, 默认值是一个空Object对象</li>
<li>对象的[<code>__ proto __</code>]属性: 创建对象时自动添加的, <code>默认值为构造函数的prototype属性值</code></li>
<li>程序员能直接操作显式原型, 但不能直接操作隐式原型(ES6之前)</li>
</ul>
<p>  5.代码示例：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//定义构造函数</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Fn</span>(<span class="params"></span>) &#123;</span><br><span class="line"> <span class="comment">// 内部默认执行语句: this.prototype = &#123;&#125;</span></span><br><span class="line">  &#125;</span><br><span class="line"><span class="comment">// 1. 每个函数function都有一个prototype，即显式原型属性, 默认指向一个空的Object对象</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Fn</span>.<span class="property"><span class="keyword">prototype</span></span>)</span><br><span class="line"><span class="comment">// 2. 每个实例对象都有一个__proto__，可称为隐式原型</span></span><br><span class="line"><span class="comment">//创建实例对象</span></span><br><span class="line"><span class="keyword">var</span> fn = <span class="keyword">new</span> <span class="title class_">Fn</span>()  <span class="comment">// 内部默认执行语句: this.__proto__ = Fn.prototype</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(fn.<span class="property">__proto__</span>)</span><br><span class="line"><span class="comment">// 3. 对象的隐式原型的值为其对应构造函数的显式原型的值</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Fn</span>.<span class="property"><span class="keyword">prototype</span></span>===fn.<span class="property">__proto__</span>) <span class="comment">// true</span></span><br><span class="line"><span class="comment">//给原型添加方法</span></span><br><span class="line"><span class="title class_">Fn</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">test</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">  <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;test()&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//通过实例调用原型的方法</span></span><br><span class="line">fn.<span class="title function_">test</span>()</span><br></pre></td></tr></table></figure>



<h4 id="3-constructor"><a href="#3-constructor" class="headerlink" title="3.constructor"></a>3.constructor</h4><p>构造函数的原型中包含属性 <code>constructor</code> 指向该构造函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">User</span>(<span class="params">name</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> p1 = <span class="keyword">new</span> <span class="title class_">User</span>(<span class="string">&quot;张三&quot;</span>);</span><br><span class="line"><span class="keyword">let</span> ls = <span class="keyword">new</span> p1.<span class="title function_">constructor</span>(<span class="params"><span class="string">&quot;李四&quot;</span></span>);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(ls.<span class="property">name</span>);<span class="comment">//李四</span></span><br></pre></td></tr></table></figure>

<p>以下代码直接设置了构造函数的原型将造成 <code>constructor</code> 丢失</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">User</span>(<span class="params">name</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name;</span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">User</span>.<span class="property"><span class="keyword">prototype</span></span> = &#123;</span><br><span class="line">    <span class="comment">//constructor: User,</span></span><br><span class="line">    <span class="title function_">show</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> lisi=<span class="keyword">new</span> <span class="title class_">User</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="title function_">constructor</span>(<span class="params"><span class="string">&#x27;李四&#x27;</span></span>)</span><br><span class="line"><span class="keyword">let</span> zhangsan=<span class="keyword">new</span> <span class="title class_">User</span>(<span class="string">&#x27;张三&#x27;</span>)</span><br><span class="line">zhangsan.<span class="title function_">show</span>()<span class="comment">//hello</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(lisi.<span class="property">constructor</span>===zhangsan.<span class="property">constructor</span>);<span class="comment">//false</span></span><br><span class="line">lisi.<span class="title function_">show</span>()<span class="comment">//报错：Uncaught TypeError: lisi.show is not a function</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>解释：</p>
<p>通过对象字面量给<code>Person.prototype</code>进行赋值时，此时User的prototype发生了改变，User不再指向原来的prototype，而是指向了一个含有show()方法的新的prototype，但此时constructor丢失，新的User.prototype中没有constructor，所以李四会报错，</p>
<p>正确的做法是要保证原型中的 <code>constructor</code>指向构造函数。</p>
<p>在设置<code>User.prototype </code>时，加入代码<code>constructor: User,</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">User</span>(<span class="params">name</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name;</span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">User</span>.<span class="property"><span class="keyword">prototype</span></span> = &#123;</span><br><span class="line">    <span class="attr">constructor</span>: <span class="title class_">User</span>,</span><br><span class="line">    <span class="title function_">show</span>(<span class="params"></span>)&#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;hello&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> lisi=<span class="keyword">new</span> <span class="title class_">User</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="title function_">constructor</span>(<span class="params"><span class="string">&#x27;李四&#x27;</span></span>)</span><br><span class="line"><span class="keyword">let</span> zhangsan=<span class="keyword">new</span> <span class="title class_">User</span>(<span class="string">&#x27;张三&#x27;</span>)</span><br><span class="line">zhangsan.<span class="title function_">show</span>()<span class="comment">//hello</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(lisi.<span class="property">constructor</span>===zhangsan.<span class="property">constructor</span>);<span class="comment">//true</span></span><br><span class="line">lisi.<span class="title function_">show</span>()<span class="comment">//hello</span></span><br></pre></td></tr></table></figure>



<h3 id="二-原型链"><a href="#二-原型链" class="headerlink" title="二.原型链"></a>二.原型链</h3><ol>
<li><p>原型链:</p>
<p>​      访问一个对象的属性时，</p>
<p>​      先在自身属性中查找，找到返回</p>
<p>​      如果没有, 再沿着[__ proto __]这条链向上查找, 找到返回</p>
<p>​     如果最终没找到, 返回undefined</p>
</li>
<li><p>实例对象的隐式原型===构造函数的显式原型</p>
</li>
<li><p>函数的显示原型指向的对象默认是空Object实例对象(但Object不满足)</p>
</li>
<li><p>所有函数都是Function的实例(Function是它本身的实例)</p>
<p>自己是由自己创建的，好像不符合逻辑，但仔细想想，现实世界也有些类似，你是怎么来的，你妈生的，你妈怎么来的，你姥姥生的，……类人猿进化来的，那类人猿从哪来，一直追溯下去……，就是无，（NULL生万物）<br> 正如《道德经》里所说“无，名天地之始”。</p>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>===<span class="title class_">Function</span>.<span class="property">__proto__</span></span><br></pre></td></tr></table></figure>

<p>   5.Object的原型对象是原型链的尽头</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Fn</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">test1</span>=<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">         <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;test1()&quot;</span>);</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">Fn</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">test2</span>=<span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;test2()&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> fn=<span class="keyword">new</span> <span class="title class_">Fn</span>();</span><br><span class="line"></span><br><span class="line">fn.<span class="title function_">test1</span>();<span class="comment">//test1()</span></span><br><span class="line">fn.<span class="title function_">test2</span>();<span class="comment">//test2()</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(fn.<span class="title function_">toString</span>());<span class="comment">//[object Object]</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(fn.<span class="property">test3</span>);<span class="comment">//undefined</span></span><br><span class="line"><span class="comment">//fn.test3();  --&gt;   fn.test3 is not a function</span></span><br><span class="line"><span class="comment">//解释 test3 与 test3()</span></span><br><span class="line"><span class="comment">//这里的fn.test3相当于定义了一个属性，但它没有使用过，所以是undefined；</span></span><br><span class="line"><span class="comment">//但在后面加(),test3是一个属性，()表示函数调用，test3()是一个函数，所以会报错：fn.test3 is not a function </span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;------------------------------------&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//所有函数都是Function的实例(Function是它本身的实例)</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>===<span class="title class_">Function</span>.<span class="property">__proto__</span>);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//函数的显示原型指向的对象默认是空Object实例对象(但Object不满足)</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Fn</span>.<span class="property"><span class="keyword">prototype</span></span> <span class="keyword">instanceof</span> <span class="title class_">Object</span>);<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span> <span class="keyword">instanceof</span> <span class="title class_">Object</span>);<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span> <span class="keyword">instanceof</span> <span class="title class_">Object</span>);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//Object的原型对象是原型链的尽头</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">__proto__</span>);<span class="comment">//null</span></span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202261843700.png"></p>
<h3 id="三-原型链的属性问题"><a href="#三-原型链的属性问题" class="headerlink" title="三.原型链的属性问题"></a>三.原型链的属性问题</h3><p>1.读取对象的属性值时: 现在自身查找，如果找不到，会自动到原型链中查找</p>
<p> 2.设置对象的属性值时: 不会查找原型链, 如果当前对象中没有此属性, 直接添加此属性并设置其值</p>
<p> 3.方法一般定义在原型中, 属性一般通过构造函数定义在对象本身上</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Fn</span>(<span class="params"></span>) &#123; &#125;</span><br><span class="line"><span class="title class_">Fn</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">a</span> = <span class="string">&#x27;xxx&#x27;</span></span><br><span class="line"><span class="keyword">var</span> fn1 = <span class="keyword">new</span> <span class="title class_">Fn</span>()</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(fn1.<span class="property">a</span>, fn1) <span class="comment">//xxx Fn&#123;&#125;,此时属性a在原型链上，不在fn1上</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> fn2 = <span class="keyword">new</span> <span class="title class_">Fn</span>()</span><br><span class="line">fn2.<span class="property">a</span> = <span class="string">&#x27;yyy&#x27;</span><span class="comment">//设置属性值，直接添加在本身</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(fn1.<span class="property">a</span>, fn2.<span class="property">a</span>, fn2) <span class="comment">//xxx yyy  Fn&#123;a: &quot;yyy&quot;&#125;</span></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        Fn &#123;a: &#x27;yyy&#x27;&#125;</span></span><br><span class="line"><span class="comment">            [[Prototype]]: Object</span></span><br><span class="line"><span class="comment">                a: &quot;xxx&quot;</span></span><br><span class="line"><span class="comment">        </span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        </span><br><span class="line">        </span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Person</span>(<span class="params">name, age</span>) &#123;</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">    <span class="variable language_">this</span>.<span class="property">age</span> = age</span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">Person</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">setName</span> = <span class="keyword">function</span> (<span class="params">name</span>) &#123;</span><br><span class="line">     <span class="variable language_">this</span>.<span class="property">name</span> = name</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> p1 = <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&#x27;Tom&#x27;</span>, <span class="number">12</span>)</span><br><span class="line">p1.<span class="title function_">setName</span>(<span class="string">&#x27;Bob&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(p1)  <span class="comment">//Person &#123;name: &quot;Bob&quot;, age: 12&#125;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> p2 = <span class="keyword">new</span> <span class="title class_">Person</span>(<span class="string">&#x27;Jack&#x27;</span>, <span class="number">12</span>)</span><br><span class="line">p2.<span class="title function_">setName</span>(<span class="string">&#x27;Cat&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(p2) <span class="comment">//Person &#123;name: &quot;Cat&quot;, age: 12&#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(p1.<span class="property">__proto__</span>===p2.<span class="property">__proto__</span>) <span class="comment">// true   </span></span><br></pre></td></tr></table></figure>



<h3 id="四-instanceof判断"><a href="#四-instanceof判断" class="headerlink" title="四.instanceof判断"></a>四.instanceof判断</h3><p>1.instanceof是如何判断的?</p>
<p>​      表达式: A instanceof B</p>
<p>​      如果B函数的显式原型对象在A对象的原型链上, 返回true, 否则返回false</p>
<p>​      A是一个实例对象，B是一个构造函数</p>
<img src="[E:\note\前端\笔记\原型与原型链图片\原型链终极.jpg](https://gitee.com/mengqiuleo/images/raw/master/blog/202202261843701.jpg)" style="zoom:85%;" />

<p>解释：</p>
<p>1.构造函数是一个函数对象，是通过Function构造器产生的</p>
<p><strong>所有<em>函数对象</em>的</strong>proto<strong>都指向Function.prototype</strong>,它是一个空函数（Empty function）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Foo</span>.<span class="property">__proto__</span>===<span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>);<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Foo</span>.<span class="property">constructor</span>);<span class="comment">//ƒ Function() &#123; [native code] &#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>);<span class="comment">//ƒ () &#123; [native code] &#125;</span></span><br><span class="line"><span class="comment">//Function.prototypes是一个空函数</span></span><br></pre></td></tr></table></figure>

<p>2.原型对象本身是一个普通对象(实例对象)，而普通对象的构造函数都是<code>Object</code></p>
<p>所有的构造器也都是一个普通 JS 对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//实例对象的隐式原型===构造函数的显式原型</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Foo</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">__proto__</span>===<span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>);<span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<p>3.所有的构造器都是函数对象，并且Object是一个构造函数，函数对象都是 <code>Function</code>构造产生的</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//实例对象的隐式原型===构造函数的显式原型</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span>.<span class="property">__proto__</span> === <span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>);</span><br></pre></td></tr></table></figure>

<p>4.所有函数都是Function的实例(Function是它本身的实例)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>===<span class="title class_">Function</span>.<span class="property">__proto__</span></span><br></pre></td></tr></table></figure>

<p>5.<code>Function.prototype</code>也是唯一一个<code>typeof XXX.prototype</code>为 <code>function</code>的<code>prototype</code>。其它的构造器的<code>prototype</code>都是一个对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>) <span class="comment">// function</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>)   <span class="comment">// object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Number</span>.<span class="property"><span class="keyword">prototype</span></span>)   <span class="comment">// object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Boolean</span>.<span class="property"><span class="keyword">prototype</span></span>)  <span class="comment">// object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">String</span>.<span class="property"><span class="keyword">prototype</span></span>)   <span class="comment">// object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>)    <span class="comment">// object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">RegExp</span>.<span class="property"><span class="keyword">prototype</span></span>)   <span class="comment">// object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Error</span>.<span class="property"><span class="keyword">prototype</span></span>)    <span class="comment">// object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Date</span>.<span class="property"><span class="keyword">prototype</span></span>)     <span class="comment">// object</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="keyword">typeof</span> <span class="title class_">Foo</span>.<span class="property"><span class="keyword">prototype</span></span>)   <span class="comment">// object</span></span><br></pre></td></tr></table></figure>



<p>举例1：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">Foo</span>(<span class="params"></span>) &#123;  &#125;</span><br><span class="line"><span class="keyword">var</span> f1 = <span class="keyword">new</span> <span class="title class_">Foo</span>()</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(f1 <span class="keyword">instanceof</span> <span class="title class_">Foo</span>) <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(f1 <span class="keyword">instanceof</span> <span class="title class_">Object</span>) <span class="comment">// true</span></span><br></pre></td></tr></table></figure>

<p>举例2：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span> <span class="keyword">instanceof</span> <span class="title class_">Function</span>) <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span> <span class="keyword">instanceof</span> <span class="title class_">Object</span>) <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Function</span> <span class="keyword">instanceof</span> <span class="title class_">Function</span>) <span class="comment">// true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Function</span> <span class="keyword">instanceof</span> <span class="title class_">Object</span>) <span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">Foo</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span> <span class="keyword">instanceof</span>  <span class="title class_">Foo</span>) <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<p>举例3：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">A</span> () &#123;&#125;</span><br><span class="line">A.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">n</span> = <span class="number">1</span></span><br><span class="line"><span class="keyword">let</span> b = <span class="keyword">new</span> <span class="title function_">A</span>()</span><br><span class="line">A.<span class="property"><span class="keyword">prototype</span></span> = &#123; <span class="attr">n</span>: <span class="number">2</span>, <span class="attr">m</span>: <span class="number">3</span>&#125;<span class="comment">//这里并不是增加，而是重写</span></span><br><span class="line"><span class="keyword">let</span> c = <span class="keyword">new</span> <span class="title function_">A</span>()</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(b.<span class="property">n</span>, b.<span class="property">m</span>, c.<span class="property">n</span>, c.<span class="property">m</span>) <span class="comment">// 1 undefined 2 3</span></span><br></pre></td></tr></table></figure>

<p>举例4：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">F</span> ()&#123;&#125;</span><br><span class="line"><span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">a</span> = <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;a()&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">b</span> = <span class="keyword">function</span>(<span class="params"></span>)&#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;b()&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//a()定义在Object的原型上，在源头上，无论怎么调用其实都能找到</span></span><br><span class="line"><span class="comment">//总结一句话：只要在Object原型上定义的方法，任何函数和对象都能看到</span></span><br><span class="line"><span class="keyword">let</span> f = <span class="keyword">new</span> <span class="title function_">F</span>()</span><br><span class="line">f.<span class="title function_">a</span>() <span class="comment">//a()</span></span><br><span class="line">f.<span class="title function_">b</span>() <span class="comment">//f.b is not a function --&gt;找不到</span></span><br><span class="line">F.<span class="title function_">a</span>() <span class="comment">//a()</span></span><br><span class="line">F.<span class="title function_">b</span>() <span class="comment">//b()</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(f) <span class="comment">//F &#123;&#125;</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>) </span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span>)</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202261843698.png"></p>
<h3 id="五-相关方法"><a href="#五-相关方法" class="headerlink" title="五.相关方法"></a>五.相关方法</h3><h5 id="1-Object-getPrototypeOf"><a href="#1-Object-getPrototypeOf" class="headerlink" title="1.Object.getPrototypeOf()"></a>1.Object.getPrototypeOf()</h5><p><code>Object.getPrototypeOf</code>方法返回一个对象的原型。这是获取原型对象的标准方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 空对象的原型是Object.prototype</span></span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">getPrototypeOf</span>(&#123;&#125;) === <span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span></span><br><span class="line"><span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 函数的原型是Function.prototype</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">f</span>(<span class="params"></span>) &#123;&#125;</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">getPrototypeOf</span>(f) === <span class="title class_">Function</span>.<span class="property"><span class="keyword">prototype</span></span></span><br><span class="line"><span class="comment">// true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// f 为 F 的实例对象，则 f 的原型是 F.prototype</span></span><br><span class="line"><span class="keyword">var</span> f = <span class="keyword">new</span> <span class="title function_">F</span>();</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">getPrototypeOf</span>(f) === F.<span class="property"><span class="keyword">prototype</span></span></span><br><span class="line"><span class="comment">// true</span></span><br></pre></td></tr></table></figure>



<h5 id="2-Object-setPrototypeOf"><a href="#2-Object-setPrototypeOf" class="headerlink" title="2.Object.setPrototypeOf()"></a>2.Object.setPrototypeOf()</h5><p><code>Object.setPrototypeOf</code>方法可以为现有对象设置原型，返回一个新对象。</p>
<p><code>Object.setPrototypeOf</code>方法接受两个参数，第一个是现有对象，第二个是原型对象。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> a = &#123;<span class="attr">x</span>: <span class="number">1</span>&#125;;</span><br><span class="line"><span class="keyword">var</span> b = <span class="title class_">Object</span>.<span class="title function_">setPrototypeOf</span>(&#123;&#125;, a);</span><br><span class="line"><span class="comment">// 等同于</span></span><br><span class="line"><span class="comment">// var b = &#123;__proto__: a&#125;;</span></span><br><span class="line"></span><br><span class="line">b.<span class="property">x</span> <span class="comment">// 1</span></span><br></pre></td></tr></table></figure>

<p>上面代码中，<code>b</code>对象是<code>Object.setPrototypeOf</code>方法返回的一个新对象。该对象本身为空、原型为<code>a</code>对象，所以<code>b</code>对象可以拿到<code>a</code>对象的所有属性和方法。<code>b</code>对象本身并没有<code>x</code>属性，但是 JavaScript 引擎找到它的原型对象<code>a</code>，然后读取<code>a</code>的<code>x</code>属性。</p>
<p>举例2：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="string">&quot;xiaofeixia&quot;</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> pjy = &#123;</span><br><span class="line">  <span class="attr">web</span>: <span class="string">&quot;Pan&quot;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//让obj继承hd，即设置obj的原型为hd</span></span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">setPrototypeOf</span>(obj, pjy);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(obj.<span class="property">web</span>);<span class="comment">//Pan</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span>.<span class="title function_">getPrototypeOf</span>(obj) == pjy); <span class="comment">//true</span></span><br></pre></td></tr></table></figure>



<h5 id="3-Object-create"><a href="#3-Object-create" class="headerlink" title="3.Object.create()"></a>3.Object.create()</h5><p>该方法接受一个对象作为参数，然后以它为原型，返回一个实例对象。该实例完全继承原型对象的属性。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 原型对象</span></span><br><span class="line"><span class="keyword">var</span> A = &#123;</span><br><span class="line">  <span class="attr">print</span>: <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;hello&#x27;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 实例对象</span></span><br><span class="line"><span class="keyword">var</span> B = <span class="title class_">Object</span>.<span class="title function_">create</span>(A);</span><br><span class="line">B.<span class="title function_">print</span>() <span class="comment">// hello</span></span><br><span class="line">B.<span class="property">print</span> === A.<span class="property">print</span> <span class="comment">// true</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>上面代码中，<code>Object.create</code>方法以<code>A</code>对象为原型，生成了<code>B</code>对象。<code>B</code>继承了<code>A</code>的所有属性和方法。这段代码等同于下面的代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> A = <span class="keyword">function</span> (<span class="params"></span>) &#123;&#125;;</span><br><span class="line">A.<span class="property"><span class="keyword">prototype</span></span> = &#123;</span><br><span class="line"> <span class="attr">print</span>: <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">   <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;hello&#x27;</span>);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> B = <span class="keyword">new</span> <span class="title function_">A</span>();</span><br><span class="line">B.<span class="property">print</span> === A.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">print</span> <span class="comment">// true</span></span><br></pre></td></tr></table></figure>



<p>可以通过Object.create()方法创建一个极简对象（纯数据字典对象），没有原型（原型为null)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj=<span class="title class_">Object</span>.<span class="title function_">create</span>(<span class="literal">null</span>);</span><br><span class="line"><span class="comment">//通过Object.getPrototypeOf来检查它的原型</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">Object</span>.<span class="title function_">getPrototypeOf</span>(obj));<span class="comment">//null</span></span><br></pre></td></tr></table></figure>



<p><code>Object.create</code>方法还可以接受第二个参数。该参数是一个属性描述对象，它所描述的对象属性，会添加到实例对象，作为该对象自身的属性。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = <span class="title class_">Object</span>.<span class="title function_">create</span>(&#123;&#125;, &#123;</span><br><span class="line">  <span class="attr">p1</span>: &#123;</span><br><span class="line">    <span class="attr">value</span>: <span class="number">123</span>,</span><br><span class="line">    <span class="attr">enumerable</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">configurable</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">writable</span>: <span class="literal">true</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">p2</span>: &#123;</span><br><span class="line">    <span class="attr">value</span>: <span class="string">&#x27;abc&#x27;</span>,</span><br><span class="line">    <span class="attr">enumerable</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">configurable</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">writable</span>: <span class="literal">true</span>,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 等同于</span></span><br><span class="line"><span class="keyword">var</span> obj = <span class="title class_">Object</span>.<span class="title function_">create</span>(&#123;&#125;);</span><br><span class="line">obj.<span class="property">p1</span> = <span class="number">123</span>;</span><br><span class="line">obj.<span class="property">p2</span> = <span class="string">&#x27;abc&#x27;</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>举例2：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="title class_">Pan</span> = &#123; <span class="attr">name</span>: <span class="string">&quot;xiaofeixia&quot;</span> &#125;;</span><br><span class="line"><span class="keyword">let</span> pjy = <span class="title class_">Object</span>.<span class="title function_">create</span>(<span class="title class_">Pan</span>, &#123;</span><br><span class="line">    <span class="attr">p1</span>:&#123;</span><br><span class="line">        <span class="attr">value</span>: <span class="string">&quot;a person&quot;</span>,</span><br><span class="line">        <span class="attr">enumerable</span>: <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">in</span> pjy) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(key);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//打印结果</span></span><br><span class="line"><span class="comment">//p1</span></span><br><span class="line"><span class="comment">//name</span></span><br></pre></td></tr></table></figure>



<h5 id="4-Object-prototype-isPrototypeOf"><a href="#4-Object-prototype-isPrototypeOf" class="headerlink" title="4.Object.prototype.isPrototypeOf()"></a>4.Object.prototype.isPrototypeOf()</h5><p>对象实例的<code>isPrototypeOf</code>方法，用来判断一个对象是否是另一个对象的原型。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> o1 = &#123;&#125;;</span><br><span class="line"><span class="keyword">var</span> o2 = <span class="title class_">Object</span>.<span class="title function_">create</span>(o1);</span><br><span class="line"><span class="keyword">var</span> o3 = <span class="title class_">Object</span>.<span class="title function_">create</span>(o2);</span><br><span class="line"></span><br><span class="line">o2.<span class="title function_">isPrototypeOf</span>(o3) <span class="comment">// true</span></span><br><span class="line">o1.<span class="title function_">isPrototypeOf</span>(o3) <span class="comment">// true</span></span><br><span class="line"><span class="number">123456</span></span><br></pre></td></tr></table></figure>

<p>上面代码表明，只要某个对象处在原型链上，<code>isPrototypeOf</code>都返回<code>true</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="title function_">isPrototypeOf</span>(&#123;&#125;) <span class="comment">// true</span></span><br><span class="line"><span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="title function_">isPrototypeOf</span>([]) <span class="comment">// true</span></span><br><span class="line"><span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="title function_">isPrototypeOf</span>(<span class="regexp">/xyz/</span>) <span class="comment">// true</span></span><br><span class="line"><span class="title class_">Object</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="title function_">isPrototypeOf</span>(<span class="title class_">Object</span>.<span class="title function_">create</span>(<span class="literal">null</span>)) <span class="comment">// false</span></span><br></pre></td></tr></table></figure>



<h5 id="5-Object-prototype-hasOwnProperty"><a href="#5-Object-prototype-hasOwnProperty" class="headerlink" title="5.Object.prototype.hasOwnProperty()"></a>5.Object.prototype.hasOwnProperty()</h5><p>hasOwnProperty()方法用来判断某个对象是否含有指定的自身属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">obj.<span class="title function_">hasOwnProperty</span>(<span class="string">&quot;属性名&quot;</span>);<span class="comment">//实例obj是否包含有圆括号中的属性</span></span><br></pre></td></tr></table></figure>

<p>这个方法会忽略那些从原型链上继承的属性。而使用 <code>for/in</code> 遍历时同时会遍历原型上的属性。</p>
<p>而使用<code>in</code> 可以检测原型链上是否存在属性。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> a = &#123; <span class="attr">url</span>: <span class="string">&quot;xiaofeixia&quot;</span> &#125;;</span><br><span class="line"><span class="keyword">let</span> b = &#123; <span class="attr">name</span>: <span class="string">&quot;Pan&quot;</span> &#125;;</span><br><span class="line"><span class="title class_">Object</span>.<span class="title function_">setPrototypeOf</span>(a, b);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;name&quot;</span> <span class="keyword">in</span> a);<span class="comment">//true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a.<span class="title function_">hasOwnProperty</span>(<span class="string">&quot;name&quot;</span>));<span class="comment">//false</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(a.<span class="title function_">hasOwnProperty</span>(<span class="string">&quot;url&quot;</span>));<span class="comment">//true</span></span><br></pre></td></tr></table></figure>

<p>用<code>in</code>遍历：</p>
<p>使用 <code>for/in</code> 遍历时同时会遍历原型上的属性</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="title class_">Pan</span> = &#123; <span class="attr">name</span>: <span class="string">&quot;xiaofeixia&quot;</span> &#125;;</span><br><span class="line"><span class="keyword">let</span> pjy = <span class="title class_">Object</span>.<span class="title function_">create</span>(<span class="title class_">Pan</span>, &#123;</span><br><span class="line">    <span class="attr">p1</span>:&#123;</span><br><span class="line">        <span class="attr">value</span>: <span class="string">&quot;a person&quot;</span>,</span><br><span class="line">        <span class="attr">enumerable</span>: <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">const</span> key <span class="keyword">in</span> pjy) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(key);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//打印结果</span></span><br><span class="line"><span class="comment">//p1</span></span><br><span class="line"><span class="comment">//name</span></span><br></pre></td></tr></table></figure>



</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">潘静怡</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://mengqiuleo.gitee.io/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/">https://mengqiuleo.gitee.io/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://mengqiuleo.gitee.io" target="_blank">小飞侠Pan的Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JS/">JS</a><a class="post-meta__tags" href="/tags/%E5%8E%9F%E5%9E%8B%E9%93%BE/">原型链</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/26/js/%E7%BB%84%E5%90%88(Compose)%E5%87%BD%E6%95%B0/"><img class="prev-cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">组合（Compose）函数</div></div></a></div><div class="next-post pull-right"><a href="/2022/02/26/js/%E6%95%B0%E7%BB%84/"><img class="next-cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">JS基本语法之数组</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS基本语法之call/bind/apply</div></div></a></div><div><a href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/" title="JS基础之数据类型及其判断"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS基础之数据类型及其判断</div></div></a></div><div><a href="/2022/02/26/js/%E6%95%B0%E7%BB%84/" title="JS基本语法之数组"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS基本语法之数组</div></div></a></div><div><a href="/2022/02/26/js/%E7%BB%84%E5%90%88(Compose)%E5%87%BD%E6%95%B0/" title="组合（Compose）函数"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">组合（Compose）函数</div></div></a></div><div><a href="/2022/02/26/js/%E4%BD%9C%E7%94%A8%E5%9F%9F%E4%B8%8E%E5%8F%98%E9%87%8F%E6%8F%90%E5%8D%87/" title="作用域与变量提升"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">作用域与变量提升</div></div></a></div><div><a href="/2022/02/26/js%E5%BC%82%E6%AD%A5/%E5%BC%82%E5%B8%B8error/" title="JS中的异常error处理"><img class="cover" src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-26</div><div class="title">JS中的异常error处理</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252103634.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">潘静怡</div><div class="author-info__description">一路春光, 一路荆棘, 不虚此行</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">15</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_52834435?type=blog"><i class="iconfont icon-csdn1"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://blog.csdn.net/weixin_52834435?spm=1000.2115.3001.5343" target="_blank" title="CSDN"><i class="iconfont icon-csdn1"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo" target="_blank" title="Gitee"><i class="iconfont icon-gitee2"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130203.jpg" target="_blank" title="QQ"><i class="fa-brands fa-qq"></i></a><a class="social-icon" href="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130201.jpg" target="_blank" title="WeChat"><i class="fa-brands fa-weixin"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80-%E5%8E%9F%E5%9E%8B-prototype"><span class="toc-number">1.</span> <span class="toc-text">一.原型[prototype]</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%87%BD%E6%95%B0%E7%9A%84prototype%E5%B1%9E%E6%80%A7"><span class="toc-number">1.1.</span> <span class="toc-text">1.函数的prototype属性</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E6%98%BE%E7%A4%BA%E5%8E%9F%E5%9E%8B%E4%B8%8E%E9%9A%90%E5%BC%8F%E5%8E%9F%E5%9E%8B"><span class="toc-number">1.2.</span> <span class="toc-text">2.显示原型与隐式原型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-constructor"><span class="toc-number">1.3.</span> <span class="toc-text">3.constructor</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%8C-%E5%8E%9F%E5%9E%8B%E9%93%BE"><span class="toc-number">2.</span> <span class="toc-text">二.原型链</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89-%E5%8E%9F%E5%9E%8B%E9%93%BE%E7%9A%84%E5%B1%9E%E6%80%A7%E9%97%AE%E9%A2%98"><span class="toc-number">3.</span> <span class="toc-text">三.原型链的属性问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%9B-instanceof%E5%88%A4%E6%96%AD"><span class="toc-number">4.</span> <span class="toc-text">四.instanceof判断</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BA%94-%E7%9B%B8%E5%85%B3%E6%96%B9%E6%B3%95"><span class="toc-number">5.</span> <span class="toc-text">五.相关方法</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-Object-getPrototypeOf"><span class="toc-number">5.0.1.</span> <span class="toc-text">1.Object.getPrototypeOf()</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-Object-setPrototypeOf"><span class="toc-number">5.0.2.</span> <span class="toc-text">2.Object.setPrototypeOf()</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-Object-create"><span class="toc-number">5.0.3.</span> <span class="toc-text">3.Object.create()</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-Object-prototype-isPrototypeOf"><span class="toc-number">5.0.4.</span> <span class="toc-text">4.Object.prototype.isPrototypeOf()</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-Object-prototype-hasOwnProperty"><span class="toc-number">5.0.5.</span> <span class="toc-text">5.Object.prototype.hasOwnProperty()</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/ajax/ajax/" title="JS异步之ajax"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS异步之ajax"/></a><div class="content"><a class="title" href="/2022/02/26/ajax/ajax/" title="JS异步之ajax">JS异步之ajax</a><time datetime="2022-02-26T10:53:56.362Z" title="发表于 2022-02-26 18:53:56">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基本语法之call/bind/apply"/></a><div class="content"><a class="title" href="/2022/02/26/js/call%E5%92%8Cbind%E5%92%8Capply/" title="JS基本语法之call/bind/apply">JS基本语法之call/bind/apply</a><time datetime="2022-02-26T10:52:29.044Z" title="发表于 2022-02-26 18:52:29">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/" title="JS基础之数据类型及其判断"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基础之数据类型及其判断"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E5%85%B6%E5%88%A4%E6%96%AD/" title="JS基础之数据类型及其判断">JS基础之数据类型及其判断</a><time datetime="2022-02-26T10:49:21.438Z" title="发表于 2022-02-26 18:49:21">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E6%95%B0%E7%BB%84/" title="JS基本语法之数组"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="JS基本语法之数组"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E6%95%B0%E7%BB%84/" title="JS基本语法之数组">JS基本语法之数组</a><time datetime="2022-02-26T10:47:39.357Z" title="发表于 2022-02-26 18:47:39">2022-02-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" title="原型与原型链"><img src="https://gitee.com/mengqiuleo/images/raw/master/blog/202202252130198.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="原型与原型链"/></a><div class="content"><a class="title" href="/2022/02/26/js/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" title="原型与原型链">原型与原型链</a><time datetime="2022-02-26T10:42:32.957Z" title="发表于 2022-02-26 18:42:32">2022-02-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 By 潘静怡</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">welcome to my blog!🤪</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (true){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: '',
      appKey: '',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><script src="/js/mobile.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start -->
  <script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/hexo-github-calendar@1.21/hexo_githubcalendar.js"></script>
  <script data-pjax>
        function GithubCalendarConfig(){
            var git_githubapiurl ="https://python-gitee-calendar-api.vercel.app/api?mengqiuleo";
            var git_color =['#ebedf0', '#f0fff4', '#dcffe4', '#bef5cb', '#85e89d', '#34d058', '#28a745', '#22863a', '#176f2c', '#165c26', '#144620'];
            var git_user ="mengqiuleo";
            var parent_div_git = document.getElementById('recent-posts');
            var git_div_html = '<div class="recent-post-item" style="width:100%;height:auto;padding:10px;"><div id="github_loading" style="width:10%;height:100%;margin:0 auto;display: block"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"  viewBox="0 0 50 50" style="enable-background:new 0 0 50 50" xml:space="preserve"><path fill="#d0d0d0" d="M25.251,6.461c-10.318,0-18.683,8.365-18.683,18.683h4.068c0-8.071,6.543-14.615,14.615-14.615V6.461z" transform="rotate(275.098 25 25)"><animateTransform attributeType="xml" attributeName="transform" type="rotate" from="0 25 25" to="360 25 25" dur="0.6s" repeatCount="indefinite"></animateTransform></path></svg></div><div id="github_container"></div></div>';
            if(parent_div_git && location.pathname =='/'){
                console.log('已挂载github calendar')
                // parent_div_git.innerHTML=git_div_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",git_div_html) // 有报错，但不影响使用(支持pjax跳转)
            };
            GithubCalendar(git_githubapiurl,git_color,git_user)
        }
        if(document.getElementById('recent-posts')){
            GithubCalendarConfig()
        }
    </script>
    <style>#github_container{min-height:280px}@media screen and (max-width:650px) {#github_container{background-image:;min-height:0px}}</style>
    <style></style><!-- hexo injector body_end end --></body></html>